package com.foreveross.fetl.web.action.preprocess;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import javax.inject.Inject;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.dayatang.querychannel.support.Page;
import com.foreveross.fetl.application.IFetlDataApplication;
import com.foreveross.fetl.core.domain.freight.CrawlTaskTemplateEntity;
import com.foreveross.fetl.vo.CrawlTaskTemplateVO;
import com.foreveross.fetl.vo.FetlDataVO;
import com.foreveross.fetl.web.action.BaseAction;

@Controller("airLineConfigAction")
@Scope("prototype")
public class AirLineConfigAction extends BaseAction {
	private static final long serialVersionUID = 8128199311100810896L;

	private FetlDataVO fetlDataVO = new FetlDataVO();

	private CrawlTaskTemplateVO crawlTaskTemplateVO = new CrawlTaskTemplateVO();

	@Inject
	private IFetlDataApplication fetlDataApplication;

	public String list() {
		return METHOD;
	}

	public String pageJson() {
		String attachHbaseKey = null;
		String fromCity = null;
		String toCity = null;
		if (null != fetlDataVO) {
			attachHbaseKey = fetlDataVO.getAttachHbaseKey();
			fromCity = fetlDataVO.getFromCity();
			toCity = fetlDataVO.getToCity();
		}
		Page<CrawlTaskTemplateVO> all = fetlDataApplication.pageQuery(
				attachHbaseKey, fromCity, toCity, page, pagesize);
		dataMap.put("Rows", all.getResult());
		dataMap.put("start", page * pagesize - pagesize);
		dataMap.put("limit", pagesize);
		dataMap.put("Total", all.getTotalCount());
		return JSON;
	}

	public String update() {
		String initIds = getRequest().getParameter("initIds");
		String selectedIds = getRequest().getParameter("selectedIds");
		try {
			Integer[] deleteIds = minus(initIds, selectedIds);
			Integer[] saveIds = minus(selectedIds, initIds);
			for (int i = 0; i < saveIds.length; i++) {
				CrawlTaskTemplateEntity entity = new CrawlTaskTemplateEntity();
				entity = (CrawlTaskTemplateEntity) entity.get(saveIds[i]);
				FetlDataVO vo = new FetlDataVO();
				vo.setAttachHbaseKey(entity.getAttachHbaseKey());
				vo.setFromCity(entity.getFromCity());
				vo.setToCity(entity.getToCity());
				fetlDataApplication.save(vo);
			}
			for (int i = 0; i < deleteIds.length; i++) {
				// 查找出该航线数据
				CrawlTaskTemplateEntity entity = new CrawlTaskTemplateEntity();
				entity = (CrawlTaskTemplateEntity) entity.get(deleteIds[i]);
				List<FetlDataVO> vos = fetlDataApplication.find(
						entity.getAttachHbaseKey(), entity.getFromCity(),
						entity.getToCity());
				if (vos != null && vos.size() != 0) {
					fetlDataApplication.delete(vos.get(0));
				}
			}
			dataMap.put("result", "success");
		} catch (Exception e) {
			dataMap.put("result", "failed");
			e.printStackTrace();
		}
		return JSON;
	}

	// 求两个数组的差集123,234,d:1,s:4
	public static Integer[] minus(String str1, String str2) {
		List<Integer> result=new LinkedList<Integer>();
		List<String> arr2=Arrays.asList(str2.split(","));
		for (String str : str1.split(",")) {
			if (!arr2.contains(str) && !"".equals(str))
				result.add(Integer.parseInt(str));
		}
		return result.toArray(new Integer[] {});
	}

	public FetlDataVO getFetlDataVO() {
		return fetlDataVO;
	}

	public void setFetlDataVO(FetlDataVO fetlDataVO) {
		this.fetlDataVO = fetlDataVO;
	}

	public CrawlTaskTemplateVO getCrawlTaskTemplateVO() {
		return crawlTaskTemplateVO;
	}

	public void setCrawlTaskTemplateVO(CrawlTaskTemplateVO crawlTaskTemplateVO) {
		this.crawlTaskTemplateVO = crawlTaskTemplateVO;
	}

}